<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <div class="login-form">
        <h2 class="login-title">轻客管家</h2>
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          class="login-form-inner"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login-button"
              @click="handleLogin"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api/user'

const router = useRouter()
const route = useRoute()
const loginFormRef = ref(null)
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value.validate()
    loading.value = true
    
    // 调用登录API
    const response = await login(loginForm)
    
    // 只存储后端返回的token和用户信息
    // 根据后端返回的数据结构，存储必要的信息
    if (response.data.token) {
      localStorage.setItem('token', response.data.token)
    }
    // 存储完整的用户信息对象
    localStorage.setItem('userInfo', JSON.stringify(response.data))
    // 单独存储用户名便于在页面中直接使用
    if (response.data.username || response.data.vcLoginname || response.data.name) {
      localStorage.setItem('username', response.data.username || response.data.vcLoginname || response.data.name)
    }
    
    ElMessage.success('登录成功')
    
    // 获取重定向地址
    const redirect = route.query.redirect || '/dashboard'
    router.push(redirect)
  } catch (error) {
    // 表单验证失败不显示错误消息
    if (error !== false) {
      // 错误信息已在API拦截器中处理
      console.error('登录失败:', error)
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
}

.login-form {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  text-align: center;
}

.login-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #333;
}

.login-form-inner {
  width: 100%;
}

.login-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  border: none;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  transform: translateY(-1px);
}

.login-button:active {
  transform: translateY(0);
}

:deep(.el-input__wrapper) {
  border-radius: 6px;
}

:deep(.el-input__prefix-inner) {
  padding-left: 5px;
}

:deep(.el-input__wrapper.is-focused) {
  box-shadow: 0 0 0 1px #3b82f6 inset;
}
</style>